<?php
namespace app\user\controller\admin;

use app\admin\controller\Backend;

class Distributor extends Backend
{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user/user', 'service');
    }

    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit, $field, $extra) = $this->buildparams();
            $where = function($query) use ($where) {
                foreach ($where as $k => $v)
                {
                    if (is_array($v)) {
                        call_user_func_array([$query, 'where'], $v);
                    } else {
                        $query->where($v);
                    }
                }
            };
            $result = model('user/distributor', 'service')->lists($where,$limit,$offset, (!$sort && !$order ? '' : $sort.' '.$order), $field, $extra);
            return $this->success('Operation completed', null, $result);
        }
        return $this->view->fetch();
    }

    public function review($id = 0) {
        $model = model('user/distributor', 'service');
        if($this->request->isPost()) {
            $info = $this->request->param('row/a');
            $result = $model->review($this->request->param('id'), $info['status'], $info['manage_remark']);
            if(!$result) {
                $this->error($model->getError());
            } else {
                $this->success('操作成功');
            }
        } else {
            $row = $model->where(['id' => $id])->find();
            return $this->view
                ->assign('row', $row)
                ->fetch();
        }
    }

    public function selectpage()
    {
        $where = [
            'role' => 1,
        ];
        return parent::selectpage($where);
    }

}